package com.yanqu.road.server.cmd.tradewar.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.pb.tradewaractivity.TradeWarActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.TradeWarActivityMgr;
import com.yanqu.road.server.manger.activity.TradeWarActivityRankMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_TRADE_WAR_ACTIVITY_MEMBER_CONDITION_DATA, desc = "跨服商战商会成员贡献值")
public class CrossTradeWarMemberConditionDataCmd extends AbstractCommand {

	private int activityId;

	private String unionUid;

	private long serverId;

	private UnionActivityConditionData unionActivityConditionData;

	private boolean needResp = true;

	private TradeWarActivityProto.TradeWarActivityConditionUnionMemberDataReqMsg reqMsg;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		reqMsg = TradeWarActivityProto.TradeWarActivityConditionUnionMemberDataReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		unionUid = reqMsg.getUnionUid();
		serverId = reqMsg.getServerId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = TradeWarActivityMgr.getOpenActivityInfo(activityId);
		if(null == activityInfo){
			ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
			return;
		}
	/*	if(GameServer.getInstance().getServerId() == serverId) {
			unionActivityConditionData = TradeWarActivityRankMgr.getUnionActivityConditionData(activityId, unionUid);
			if (null == unionActivityConditionData) {
				ret = GameErrorCode.E_ACTIVITY_NO_JOIN;
				return;
			}
		}else {//到跨服查数据
			needResp = false;
			YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_TRADE_WAR_MEMBER_CONDITION_DATA, reqMsg);
			GamePlayerMgr.sendChannelPacket(player.getUserId(), pbMsg);
		}*/
		needResp = false;
//		CrossUnionActivityRankDataProto.CrossUnionMemberValueReqMsg.Builder msg = CrossUnionActivityRankDataProto.CrossUnionMemberValueReqMsg.newBuilder();
//		msg.setActivityId(activityId);
//		msg.setUnionUid(unionUid);
//		msg.setServerId(serverId);
//		msg.setType(0);
//		msg.setRankType(1);
//		YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_UNION_ACTIVITY_MEMBER_VALUE, msg);
//		GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);

		YRankProto.YRankReqMsg.Builder builder = YRankProto.YRankReqMsg.newBuilder();
		builder.setActivityId(activityInfo.getActivityId());
		builder.setGroupId(GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId()));
		builder.setIdKey(String.valueOf(eGamePlayerEventType.CrossTradeWarUnionScoreRiseRank.getValue()));
		builder.setSeeUnionUid(unionUid);
		player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_GET_UNION_RANK_CONTRIBUTION, builder, activityId);
		return;
	}

	@Override
	public void sendData(GamePlayer player) {
		if(needResp) {
			TradeWarActivityProto.TradeWarActivityConditionUnionMemberDataRespMsg.Builder respMsg = TradeWarActivityProto.TradeWarActivityConditionUnionMemberDataRespMsg.newBuilder();
			respMsg.setRet(ret);
			if (0 == ret) {
				if (null != unionActivityConditionData) {
					ActivityProto.UnionRankMemberConditionDataListMsg.Builder dataMsg = ActivityPb.parseUnionRankMemberConditionDataListMsg(
							UnionActivityMgr.getUserActivityConditionDataList(unionActivityConditionData, TradeWarActivityRankMgr.getUserActivityConditionDataMap(activityId)));
					respMsg.setDataList(dataMsg);
				}
			}
			player.sendPacket(Protocol.U_TRADE_WAR_ACTIVITY_MEMBER_CONDITION_DATA, respMsg);
		}
	}

}
